#####################################################################
####State Capacity, Insurgency, and Civil War - F&L Country Level####
#####################################################################
library(MASS) 
library(pscl)
library(foreign)
library(Hmisc)
library(rgdal)
library(RArcInfo)
library(stargazer)
library(mvtnorm)
library(Zelig)
library(readstata13)
library(pROC)
library(robust)
library(cvTools)
library(boot)
library(maps)
library(mapdata)
library(doBy)
library(raster)
library(survival)
library(spduration)
library(DAMisc)
library(plyr)
library(DataCombine)
library(plm)

########################
###Create The Dataset###
########################
###Importing the main data
#set working directory to location of dta file
setwd("~/Google Drive/NSLC/Final Dofiles")
#Import country leve data
main.data.ct <- read.dta("ntlcount.dta")

##########################
###CW Onset replication###
##########################

#########Replication without nighttime light
#Model 1
FL.1.r <- glm(onset ~warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + polity2l + ethfrac + relfrac,
                data=main.data.ct, family = "binomial")
summary(FL.1.r)
AIC(FL.1.r)

#Model 2
main.data.ct.eth <- main.data.ct[ which(main.data.ct$second > 0.04999),]
FL.2.r <- glm(onset ~ warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + polity2l + ethfrac + relfrac,
                data=main.data.ct.eth, family = "binomial")
summary(FL.2.r)
AIC(FL.2.r)

#Model 1
FL.3.r <- glm(onset ~ warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + ethfrac + relfrac + anocl + deml,
                data=main.data.ct, family = "binomial")
summary(FL.3.r)
AIC(FL.3.r)

#########Replication With nighttime light
#Model 1
FL.1 <- glm(onset ~ lnNL_country_sum + warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + polity2l + ethfrac + relfrac,
                data=main.data.ct, family = "binomial")
summary(FL.1)
AIC(FL.1)

#Model 2
main.data.ct.eth <- main.data.ct[ which(main.data.ct$second > 0.04999),]
FL.2 <- glm(onset ~ lnNL_country_sum + warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + polity2l + ethfrac + relfrac,
                data=main.data.ct.eth, family = "binomial")
summary(FL.2)
AIC(FL.2)

#Model 1
FL.3 <- glm(onset ~ lnNL_country_sum + warl + lgdpenl1 + lpopl1 + lmtnest + ncontig + Oil + 
			nwstate + instab + ethfrac + relfrac + anocl + deml,
                data=main.data.ct, family = "binomial")
summary(FL.3)
AIC(FL.3)

#Import to LaTex
stargazer(FL.1.r, FL.2.r, FL.3.r, FL.1, FL.2, FL.3)








